<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="风场名称" prop="farmName">
        <el-input
          v-model="queryParams.farmName"
          placeholder="请输入风场名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地理位置" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入地理位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运营商" prop="operator">
        <el-select v-model="queryParams.operator" placeholder="请选择运营商" clearable>
          <el-option
            v-for="dict in farm_operators"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="当前状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择当前状态" clearable>
          <el-option
            v-for="dict in farm_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['farm_manage:farm:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['farm_manage:farm:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['farm_manage:farm:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['farm_manage:farm:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="farmList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="50" type="index" prop="farmId" />
      <el-table-column label="风场名称" align="center" prop="farmName" />
      <el-table-column label="在线设备/设备总数" align="center"  prop="devicesCount" >
        <template #default="scope">
          {{scope.row.workingDevicesCount}}/{{scope.row.totalDevicesCount}}
        </template>
      </el-table-column>
      <el-table-column label="地理位置" align="center" prop="location" />
          <el-table-column label="运营商" align="center" prop="operator">
        <template #default="scope">
          <dict-tag :options="farm_operators" :value="scope.row.operator"/>
        </template>
      </el-table-column>
      <el-table-column label="投产日期" align="center" prop="commissionDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.commissionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总装机容量" align="center" prop="capacity" />
      <el-table-column label="当前状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="farm_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250px">
        <template #default="scope">
          <el-button link type="primary" icon="Document" @click="getFarmInfo(scope.row)" v-hasPermi="['device_manage:device_manage:list']">查看详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['farm_manage:farm:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['farm_manage:farm:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改风场管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="farmRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="风场名称" prop="farmName">
          <el-input v-model="form.farmName" placeholder="请输入风场名称" />
        </el-form-item>
        <el-form-item label="地理位置" prop="location" v-if="form.farmId==null">
          <el-input v-model="form.location" placeholder="请输入地理位置" />
        </el-form-item>
        <el-form-item label="运营商" prop="operator">
          <el-select v-model="form.operator" placeholder="请选择运营商">
            <el-option
              v-for="dict in farm_operators"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="投产日期" prop="commissionDate" v-if="form.farmId==null">
          <el-date-picker clearable
            v-model="form.commissionDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择投产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总装机容量" prop="capacity">
          <el-input v-model="form.capacity" placeholder="请输入总装机容量" />
        </el-form-item>
        <el-form-item label="当前状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择当前状态">
            <el-option
              v-for="dict in farm_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Farm">
import { listFarm, getFarm, delFarm, addFarm, updateFarm } from "@/api/farm_manage/farm";
import router from "@/router/index.js";

const { proxy } = getCurrentInstance();
const { farm_operators, farm_status } = proxy.useDict('farm_operators', 'farm_status');

const farmList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    farmName: null,
    location: null,
    operator: null,
    status: null,
  },
  rules: {
    farmName: [
      { required: true, message: "风场名称不能为空", trigger: "blur" }
    ],
    location: [
      { required: true, message: "地理位置不能为空", trigger: "blur" }
    ],
    operator: [
      { required: true, message: "运营商不能为空", trigger: "change" }
    ],
    commissionDate: [
      { required: true, message: "投产日期不能为空", trigger: "blur" }
    ],
    capacity: [
      { required: true, message: "总装机容量不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "当前状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询风场管理列表 */
function getList() {
  loading.value = true;
  listFarm(queryParams.value).then(response => {
    farmList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    farmId: null,
    farmName: null,
    location: null,
    operator: null,
    commissionDate: null,
    capacity: null,
    status: null,
    createTime: null,
    updateTime: null,
    updateBy: null,
    remark: null,
    createBy: null
  };
  proxy.resetForm("farmRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.farmId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加风场管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _farmId = row.farmId || ids.value
  getFarm(_farmId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改风场管理";
  });
}

function getFarmInfo(row){
  reset();
  const _farmId = row.farmId
  router.push({
    path: '/farms/divice_manage/device_manage',
    query: { farmId: _farmId }, // 传递 farmId 参数
    replace: true
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["farmRef"].validate(valid => {
    if (valid) {
      if (form.value.farmId != null) {
        updateFarm(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFarm(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _farmIds = row.farmId || ids.value;
  proxy.$modal.confirm('是否确认删除风场管理编号为"' + _farmIds + '"的数据项？').then(function() {
    return delFarm(_farmIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('farm_manage/farm/export', {
    ...queryParams.value
  }, `farm_${new Date().getTime()}.xlsx`)
}

getList();
</script>


<style scoped>
.el-select{
  width: 179px;
}
</style>